// WTM默认页面 Wtm buidin page

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using HengTong.Model._Admin;
using TiaoTaoHelper.Wtm.Models;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;

namespace HengTong.ViewModel._Admin.FrameworkGroupVMs
{
    public class FrameworkGroupListVm : BasePagedListVM<FrameworkGroupView, FrameworkGroupSearcher>
    {
        public FrameworkGroupListVm()
        {
            NeedPage = false;
        }

        protected override List<GridAction> InitGridAction()
        {
            if (ConfigInfo.HasMainHost && Wtm.LoginUserInfo?.CurrentTenant == null)
            {
                return new List<GridAction>
                {
                    this.MakeAction("FrameworkGroup","DataFunction",Localizer["_Admin.DataPrivilege"],Localizer["_Admin.DataPrivilege"], GridActionParameterTypesEnum.SingleId,"_Admin",800,null,null,x=>x.GroupCode).SetShowInRow(),
                };
            }
            else
            {
                return new List<GridAction>
                {
                    this.MakeStandardAction("FrameworkGroup", GridActionStandardTypesEnum.Create, "","_Admin", dialogWidth: 800),
                    this.MakeStandardAction("FrameworkGroup", GridActionStandardTypesEnum.Edit, "","_Admin", dialogWidth: 800),
                    this.MakeStandardAction("FrameworkGroup", GridActionStandardTypesEnum.Delete, "", "_Admin",dialogWidth: 800),
                    this.MakeStandardAction("FrameworkGroup", GridActionStandardTypesEnum.BatchDelete, "","_Admin", dialogWidth: 800),
                    this.MakeStandardAction("FrameworkGroup", GridActionStandardTypesEnum.Import, "","_Admin", dialogWidth: 800),
                    this.MakeAction("FrameworkGroup","DataFunction",Localizer["_Admin.DataPrivilege"],Localizer["_Admin.DataPrivilege"], GridActionParameterTypesEnum.SingleId,"_Admin",800,null,null,x=>x.GroupCode).SetShowInRow(),
                    this.MakeStandardAction("FrameworkGroup", GridActionStandardTypesEnum.ExportExcel, "","_Admin"),
                };
            }
        }

        protected override IEnumerable<IGridColumn<FrameworkGroupView>> InitGridHeader()
        {
            return new List<GridColumn<FrameworkGroupView>>{
                this.MakeGridHeader(x => x.GroupName, 220),
                this.MakeGridHeader(x => x.GroupCode, 120),
                this.MakeGridHeader(x => x.ManagerName,220).SetFormat((a,b)=>{
                    string rv = "";
                    if(string.IsNullOrEmpty(a.ManagerName) == false)
                    {
                        rv += a.ManagerName;
                    }
                    if(string.IsNullOrEmpty(a.Manager) == false)
                    {
                        rv += $"({a.Manager})";
                    }
                    return rv;
                }),
              this.MakeGridHeader(x => x.GroupRemark),
              this.MakeGridHeader(x => x.ParentId).SetHide(),
              this.MakeGridHeader(x => x.IsDisabled),
              this.MakeGridHeader(x => x.Order).SetFormat((entity, val) => {
                  return entity.Order == null ? "" : ((decimal)entity.Order).ToString("0.##");
              }),
              this.MakeGridHeaderAction(width: 300)
            };
        }

        public override IOrderedQueryable<FrameworkGroupView> GetSearchQuery()
        {
            return DC.Set<SysGroup>()
                .CheckContain(Searcher.GroupCode, x => x.GroupCode)
                .CheckContain(Searcher.GroupName, x => x.GroupName)
                .CheckWhere(Searcher.OrgId, x => x.Organizations.Any(e => e.OrgId == Searcher.OrgId))
                .Select(e => new FrameworkGroupView
                {
                    ID = e.ID,
                    ParentId = e.ParentId,
                    GroupCode = e.GroupCode,
                    GroupName = e.GroupName,
                    Manager = e.Manager,
                    IsDisabled = e.IsDisabled,
                    Order = e.Order
                })
                .OrderBy(x => x.Order ?? 99.99m)
                .ThenBy(x => x.ID);
        }
        public override void AfterDoSearcher()
        {
            var topData = EntityList.MakeTree(x => x.GroupCode).FlatTree(x => x.Order);
            if (ControllerName.Contains("/api") == false)
            {
                topData.ForEach((x) => { int l = x.GetLevel(); for (int i = 0; i < l; i++) { x.GroupName = "&nbsp;&nbsp;&nbsp;&nbsp;" + x.GroupName; } });
            }
            EntityList = topData;

        }
    }
    public class FrameworkGroupView : TreePoco<FrameworkGroupView>
    {
        [Display(Name = "_Admin.GroupCode")]
        public string GroupCode { get; set; }

        [Display(Name = "_Admin.GroupName")]
        public string GroupName { get; set; }

        [Display(Name = "_Admin.Remark")]
        public string GroupRemark { get; set; }

        [Display(Name = "_Admin.GroupManager")]
        public string Manager { get; set; }

        [Display(Name = "_Admin.Tenant")]
        public string TenantCode { get; set; }

        [Display(Name = "_Admin.GroupManager")]
        public string ManagerName { get; set; }

        public bool HasChild { get => HasChildren; }
        
        [Display(Name = "是否公司")]
        public bool IsCompany { get; set; }
        
        [Display(Name = "是否禁用")]
        public bool IsDisabled { get; set; }
        
        [Display(Name = "排序")]
        public decimal? Order { get; set; }
    }

}
